#define _CRT_SECURE_NO_WARNINGS 1
#include "Date.h"
Date::Date(int year, int month, int day)
	:_year(year)
	, _month(month)
	, _day(day)
{
	//判断日期有效性
	if (!this->DateValidity())
		cout << "您输入的日期无效,请检查!!!" << endl;
}
Date::~Date()
{
	_year = _month = _day = 0;
}
bool Date::isLeapYear()
{
	if ((_year % 4 == 0 && _year % 100 != 0) || (_year % 400 == 0)) return true;
	return false;
}
int Date::GetDays()
{
	int days[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
	if (_month == 2 && this->isLeapYear()) return 29;
	return days[_month];
}
bool Date::DateValidity()
{
	if ((_month <= 0 || _month >= 13) || (_day <= 0 || _day > this->GetDays()))
		return false;
	return true;
}
Date::Date(const Date& d)
{
	_year = d._year;
	_month = d._month;
	_day = d._day;
}
bool Date::operator<(const Date& d)
{
	if (_year < d._year)
		return true;
	else if (_year == d._year && _month < d._month)
		return true;
	else if (_year == d._year && _month == d._month && _day < d._day)
		return true;
	return false;
}
bool Date::operator<=(const Date& d)
{
	return !this->operator>(d);
}
bool Date::operator>(const Date& d)
{
	if (_year > d._year)
		return true;
	else if (_year == d._year && _month > d._month)
		return true;
	else if (_year == d._year && _month == d._month && _day > d._day)
		return true;
	return false;
}
bool Date::operator>=(const Date& d)
{
	return !this->operator<(d);
}
bool Date::operator==(const Date& d)
{
	return !(this->operator<(d) || this->operator>(d));
}
bool Date::operator!=(const Date& d)
{
	return !this->operator==(d);
}
Date& Date::operator+=(int day)
{
	_day += day;
	while (_day > this->GetDays())
	{
		_day -= this->GetDays(); 
		_month += 1;
		if (_month > 12)
		{
			_year += 1;
			_month = 1;
		}
	}
	return *this;
}
Date Date::operator+(int day)
{
	Date date = *this;
	date += day;
	return date;
}
Date& Date::operator-=(int day)
{
	_day -= day;
	while (_day <= 0)
	{
		_month -= 1;
		if (_month <= 0)
		{
			_year -= 1;
			_month = 12;
		}
		_day += this->GetDays();
	}
	return *this;
}
Date Date::operator-(int day)
{
	Date date = *this;
	date -= day;
	return date;
}
Date& Date::operator++()
{
	*this += 1;
	return *this;
}
Date Date::operator++(int)
{
	Date date = *this;
	*this += 1;
	return date;
}
Date& Date::operator--()
{
	*this -= 1;
	return *this;
}
Date Date::operator--(int)
{
	Date date = *this;
	*this -= 1;
	return date;
}
Date& Date::operator=(const Date& d)
{
	_year = d._year;
	_month = d._month;
	_day = d._day;
	return *this;
}
int Date::operator-(Date& d)
{
	//计算哪个日期大,哪个日期小
	Date MaxDate = *this;
	Date MinDate = d;
	if (*this < d)
	{
		MaxDate = d;
		MinDate = *this;
	}
	else if (*this == d) return 0;
	int MaxDay = 0, MinDay = 0;
	MaxDay += MaxDate._day - 1;
	MinDay += MinDate._day - 1;
	MaxDate._month--;
	MinDate._month--;
	while (MaxDate._month)
	{
		MaxDay += MaxDate.GetDays();
		MaxDate._month--;
	}
	while (MinDate._month)
	{
		MinDay += MinDate.GetDays();
		MinDate._month--;
	}
	int DateYear = MaxDate._year - MinDate._year;
	int LeapYear = 0;
	if (MaxDate.isLeapYear()) LeapYear--;
	if (MinDate.isLeapYear()) LeapYear--;
	while (MaxDate._year != MinDate._year)
	{
		if (MaxDate.isLeapYear())
			LeapYear++;
		MaxDate._year--;
	}
 	return DateYear * 365 + LeapYear + MaxDay - MinDay;
}
ostream& operator<<(ostream& out, const Date& d)
{
	out << d._year << "-" << d._month << "-" << d._day;
	return out;
}
istream& operator>>(istream& in, Date& d)
{
	cout << "请依次输入年月日:>";
	in >> d._year >> d._month >> d._day;
	return in;
}
